//
// Copyright 2010 Yu Jianrong, All Rights Reserved
// 
// Licensed under BSD License.


// special case, remove iframe of dm5 page (not include the comic page)
if (window.location.host==="www.dm5.com" && !(/^\/Comic\//.test(window.location.pathname)))
{
    var ifs=document.getElementsByTagName("iframe");
    for(var i=ifs.length-1; i>=0; --i)
        ifs[i].parentNode.removeChild(ifs[i]);
}
else
($___=function()
{
    var _window = window;
    var _document = document;
    var $_=function(id)
    {
        return _document.getElementById(id);
    };
    var m=_document.createElement("meta");
    m.name="viewport";
    var c_page=0;
    var div_imgs=[];
    var VerticalWritting = true;
    var Landscape= false;

    var getPageURL=function(thePage) { return _window.location.href; };
    var div_postprocess = function(){};

    var configuration = localStorage.getItem("config")? JSON.parse(localStorage.getItem("config")):{};

    var getCurrentPage,
        getPageCount,
        getImgURL,
        setCurrentPage,
        getPrevChapter,
        getNextChapter;

    var parseEngine={
        "dm5.com":function()
        {
            getCurrentPage=function()
            {
                return page;
            };
            getPageCount=function()
            {
                return array_img.length-1;
            };
            getImgURL=function(thePage)
            {
                var oldPage = page;
                page=thePage;
                array_img=desMe();
                var retURL = uncMe(array_img[page]);
                page = oldPage;
                return retURL;
            };
            setCurrentPage=function(thePage)
            {
                page = thePage;
            };
            getPageURL = function(thePage)
            {
                var nowurl=new String(_window.location.href);
                if(nowurl.indexOf("_page")>-1)
                    nowurl=nowurl.substring(0,nowurl.indexOf("_page"));
                else if(nowurl.indexOf(".htm")>-1)
                    nowurl=nowurl.substring(0,nowurl.indexOf(".htm"));
                nowurl += '_page-'+(thePage+1)+'.htm';
                return nowurl;
            };

            getPrevChapter = function() { return false; };
            var nurl=new String(window.location);
            var cid=nurl.substring(nurl.indexOf("-")+1,nurl.indexOf("_"));
            var nextUrl="../../tiaozhuan.aspx"+"?cid="+cid;
            getNextChapter = function() { return {text:"", href:nextUrl}; };

        },
        "u17.com":function()
        {
            VerticalWritting = false;

            getCurrentPage=function()
            {
                return u_page;
            };
            getPageCount=function()
            {
                return __htmlURL.length;
            };
            getImgURL=function(thePage)
            {
                var url=__htmlURL[thePage];
                //get the html
                var request = new XMLHttpRequest();
                request.open("GET", url, false);
                request.send();
                // request.responseText;
                return (/src="([^"]*)"/).exec(eee((/document.write\(eee\('([^']*)'/).exec(request.responseText)[1]))[1];
            };
            setCurrentPage=function(thePage)
            {
                u_page=thePage;
            };
            getPageURL = function(thePage)
            {
                return __htmlURL[thePage];
            };

            // build the htmlURLs
            if (!_window.__htmlURL)
            {
                u_page = 0;
                __htmlURL=[];
                var UrlBase = document.body.innerHTML.substr(document.body.innerHTML.indexOf("$('#comicShow')")).match(/location.href=(.*?)\+\$\("#comicShow"\).*?;/)[1];
                for (var i=0;i<$_('comicShow').options.length;++i)
                {
                    __htmlURL.push(eval(UrlBase)+$_('comicShow').options[i].value+'.html');
                    if ($_('comicShow').options[i].selected)
                        u_page=i;
                }
            };
            try{
                var btns = document.querySelectorAll(".comic_read_top dd.read_btn input");

                var ChapterInfo={next:false, prev:false};
                var nextUrl = String(btns[0].onclick).match(/href='(.*?)'/);
                if (nextUrl)
                    ChapterInfo.next ={text:"", href:nextUrl[1]};
                var prevUrl = String(btns[1].onclick).match(/href='(.*?)'/);
                if (prevUrl)
                    ChapterInfo.prev ={text:"", href:prevUrl[1]};

                getPrevChapter = function()
                {
                    return ChapterInfo.prev;
                };
                getNextChapter = function()
                {
                    return ChapterInfo.next;
                };
            } catch(E)
            {
                getPrevChapter = function() { return false; };
                getNextChapter = function() { return false; };
            }
        },
        "www.boston.com/bigpicture":function()
        {
            VerticalWritting = false;
            Landscape = true;

            if (!_window.__imgs)
            {
                u_page = 0;
                __imgs=[];
                var bpDivs = _document.getElementsByClassName("bpBoth");
                for (var i=0; i<bpDivs.length;++i)
                {
                    var photoNum=0;
                    try{ photoNum= bpDivs[i].getElementsByClassName("photoNum")[0].innerText;}catch(e){};
                    var cap = bpDivs[i].getElementsByClassName("bpCaption")[0].innerText;
                    if (photoNum)
                        cap = cap.substring(photoNum.length);
                    __imgs.push(
                        {
                            url:bpDivs[i].getElementsByClassName("bpImage")[0].src, 
                            // photoNum:bpDivs[i].getElementsByClassName("photoNum")[0].innerText,
                            // caption:bpDivs[i].getElementsByClassName("bpCaption")[0].childNodes[1].nodeValue 
                            caption:cap
                    });
                }
            };

            getCurrentPage=function()
            {
                return u_page;
            };
            getPageCount=function()
            {
                return __imgs.length;
            };
            getImgURL=function(thePage)
            {
                return __imgs[thePage].url;
            };
            setCurrentPage=function(thePage)
            {
                u_page=thePage;
            };

            div_postprocess = function(theDivImg)
            {
                theDivImg.appendChild(_document.createElement("br"));
                theDivImg.appendChild(_document.createTextNode(__imgs[theDivImg.page].caption));
                theDivImg.loadImg=function()
                {
                    var _this=this;
                    if(!this.unloaded)
                    {
                        return;
                    }
                    this.unloaded=false;
                    this.tImg.onload=function()
                    {
                        var _w=this.width;
                        var _h=this.height;
                        if (_this.scrollHeight>690)
                        {
                            this.height=this.height - (_this.scrollHeight-690);
                            this.width=Math.floor(_w*this.height/_h);
                        }
                        this.onload=null;
                    };
                    setTimeout(function() {
                            if (!_this.tImg.flink)
                                _this.tImg.flink=getImgURL(_this.page);
                            _this.tImg.src=_this.tImg.flink;
                        } ,0);
                    // this.className="bpCaption";
                    // this.tImg.style.position="relative";
                    // this.tImg.style.top="0px";
                    // this.tImg.style.left="0px";
                };
            };

            getPrevChapter = function() { return false; };
            getNextChapter = function() { return false; };

        },
        "www.imanhua.com/comic":function()
        {
            getCurrentPage=function()
            {
                return iNo-1;
            };
            getPageCount=function()
            {
                return len;
            };
            getImgURL=function(thePage)
            {
                return imanhua_host + encodeURI(pic[thePage]);
            };
            setCurrentPage=function(thePage)
            {
                iNo=thePage+1;
            };
            getPageURL = function(thePage)
            {
                return base + 'p=' + String(thePage);
            };
            getPrevChapter = function() { return false; };
            getNextChapter = function() { return false; };
        },
        "www.cococomic.com/manhua|www.99comic.com/manhua":function()
        {
            getCurrentPage=function()
            {
                return parseInt(page, 10)-1;
            };
            getPageCount=function()
            {
                return datas;
            };
            getImgURL=function(thePage)
            {
                return ServerList[server-1]+arrPicListUrl[thePage];
            };
            setCurrentPage=function(thePage)
            {
                page = thePage+1;
            };
            getPageURL = function(thePage)
            {
                return '?v='+(thePage+1)+"*s="+server;
            };
            getPrevChapter = function() { return false; };
            getNextChapter = function() { return false; };
        },
        "manhua.178.com/*.shtml":function()
        {
            getCurrentPage=function()
            {
                return g_current_page-1;
            };
            getPageCount=function()
            {
                return arr_pages.length;
            };
            getImgURL=function(thePage)
            {
                return "../../imgs/"+arr_pages[thePage];
            };
            setCurrentPage=function(thePage)
            {
                g_current_page = thePage+1;
            };
            getPageURL = function(thePage)
            {
                return generatePageUrl(g_page_base,thePage+1);
            };

            var ChapterInfo={
                prev:$_("prev_chapter")?{text:$_("prev_chapter").innerText, href:$_("prev_chapter").href}:false,
                next:$_("next_chapter")?{text:$_("next_chapter").innerText, href:$_("next_chapter").href}:false};

            getPrevChapter = function()
            {
                return ChapterInfo.prev;
            };
            getNextChapter = function()
            {
                return ChapterInfo.next;
            };
        }
    };

    // if (!_window.__iPadReaderInitialized)
    // {
    //     // _window.__iPadReaderInitialized = true;
    //     // push the pages to the
    //     for (var i=0;i<getPageCount();++i)
    //     {
    //         history.pushState(i,"Page "+(i+1), getPageURL(i));
    //     }
    //     _window.onpopstate=function(e)
    //     {
    //         c_page=e.state;
    //         div_imgs[c_page].focusMe();
    //     };
    // }

    var formatStr=function(str)
    {
        var arg = arguments;
        var index = 0;
        return str.replace((/%s/g), function(s)
            {
                ++index; 
                return arg[index] === undefined? s : arg[index];
            } );
    };
    var loc_RELOAD="Reload",
        loc_VERTICALWRITTING="Vertical Writting",
        loc_CURRENTPAGE="Current page:",
        loc_JUMPTOPAGE="Jump to page",
        loc_FIRSTPAGE="First page already",
        loc_LASTPAGE="Last page already",
        loc_NOPARSER="No support for this page, follwing pages only:\n%s",
        loc_NEXTCHAPTER="Next Chapter:<br/>%s",
        loc_PREVCHAPTER="Previous Chapter:<br/>%s",
        loc_NONEXTCHAPTER="No next chapter",
        loc_NOPREVCHAPTER="no previous chapter",
        loc_LANDSCAPEWARNING="Landscape mode only, <br/>please rotate your device!",
        loc_PORTRAITWARNING="Portrait mode only, <br/>please rotate your device!";

    var loc_RELOAD="重新载入",
        loc_VERTICALWRITTING="从右向左阅读",
        loc_CURRENTPAGE="当前页：",
        loc_JUMPTOPAGE="跳转到：",
        loc_FIRSTPAGE="已经是第一页了",
        loc_LASTPAGE="已经是最后一页了",
        loc_NOPARSER="不能处理该页面，阅读器仅能处理以下页面：\n%s",
        loc_NEXTCHAPTER="下一章:<br/>%s",
        loc_PREVCHAPTER="上一章:<br/>%s",
        loc_NONEXTCHAPTER="没有下一章节",
        loc_NOPREVCHAPTER="没有上一章节",
        loc_LANDSCAPEWARNING="本页面仅能在横向模式阅读，请旋转您的设备！",
        loc_PORTRAITWARNING="本页面仅能在纵向模式阅读，请旋转您的设备！",
        loc_ANIMATIONENABLE="平滑翻页(实验中)",
        loc_ANIMATIONWARNING="注意：平滑翻页可能会造成您的Safari浏览器崩溃！\n您确定要打开平滑翻页功能吗？";


    var parserFound = false;
    for (var parser in parseEngine)
    {
        if (RegExp(parser.replace(/\.|\/|\*/g,function(e){return({".":"\.","/":"\/","*":".*"})[e];})).test(_window.location.href))
        {
            parserFound = true;
            parseEngine[parser]();
            break;
        }
    }
    if (!parserFound)
    {
        var parsers=[];
        for (var parser in parseEngine)
        {
            parsers.push.apply(parsers, parser.split("|"));
        }
        alert(formatStr(loc_NOPARSER, parsers.join("\n")));
        return;
    }

    var setConfig=function(itemName, itemValue)
    {
        configuration[itemName] = itemValue;
        localStorage.setItem("config", JSON.stringify(configuration));
    };

    // clear the images for more memory
    var emptyImageSrc="";
    var imgs = _document.getElementsByTagName("img");
    for(var i=0;i<imgs.length;++i)
        imgs[i].src = emptyImageSrc;

    if (!_window.oldTitle)
        oldTitle = _document.title;

    setTimeout(function(){
        if (!(/iPad/.test(navigator.userAgent))&&localStorage["ReaderDebug"]!=="debug")
        {
            m.content="width=device-width,initial-scale=1.0,maximum-scale=1.5,minimum-scale=0.25";
            _document.getElementsByTagName("head")[0].appendChild(m);
            _document.body.innerHTML="";
            for(i=0; i<getPageCount(); ++i)
            {
                var div_img=_document.createElement("div");
                div_img.style.cssText="visibility:hidden;position:absolute;left:0px;top:0px;";
                div_img.page=i;
                div_img.tImg=new Image();
                div_img.tImg.style.cssText="position:absolute;left:0px;top:0px;";
                div_img.appendChild(div_img.tImg);
                div_img.unloaded=true;
                div_img.eraseImg=function()
                {
                    if(!this.unloaded)
                    {
                        this.unloaded=true;
                        this.tImg.src=emptyImageSrc;
                    };
                };
                div_img.loadImg=function(focus)
                {
                    var _this=this;
                    var scrollToPos=function ()
                    {
                        if (VerticalWritting)
                        {
                            setTimeout(function(){ _window.scrollTo(_this.tImg.width-innerWidth,0); },0);
                        } else
                        {
                            setTimeout(function(){ _window.scrollTo(0,0); },0);
                        }

                    };
                    if(!this.unloaded)
                    {
                        if (focus)
                            scrollToPos();
                        return;
                    }
                    this.unloaded=false;
                    this.tImg.onload=function(){
                        if (focus)
                            scrollToPos();
                        this.onload=null;
                    };
                    setTimeout(function() {
                        if (!_this.tImg.flink)
                            _this.tImg.flink=getImgURL(_this.page);
                        _this.tImg.src=_this.tImg.flink;
                    } ,0);
                };
                div_img.focusMe=function()
                {
                    setCurrentPage(this.page);
                    div_imgs[this.page].style.visibility="visible";
                    div_imgs[this.page].loadImg(true);
                    for (var i=this.page+1; i<div_imgs.length;++i)
                    {
                        div_imgs[i].style.visibility="hidden";
                        if(i>this.page+3)
                            div_imgs[i].eraseImg();
                        else 
                            div_imgs[i].loadImg(false);
                    };
                    for (var i=this.page-1; i>=0;--i)
                    {
                        div_imgs[i].style.visibility="hidden";
                        if(i<this.page-2)
                            div_imgs[i].eraseImg();
                        else 
                            div_imgs[i].loadImg(false);
                    };
                };
                _document.body.appendChild(div_img);
                div_imgs.push(div_img);
            };
            var nextImg=function()
            {
                ++c_page;
                if(c_page>=div_imgs.length)
                {
                    c_page=div_imgs.length-1;
                    alert("Last page already");
                }else 
                    // history.forward();
                    div_imgs[c_page].focusMe();
            };
            var preImg=function()
            {
                --c_page;
                if(c_page<0)
                {
                    c_page=0;
                    alert("First page already");
                }else 
                {
                    // history.back();
                    div_imgs[c_page].focusMe();
                }
            };
            _document.onmouseup=function(e) {
                // for Firefox
                if (e.target === _document.documentElement)
                    return;

                if(!e.offsetX) {
                    e.offsetX = e.layerX;
                    e.offsetY = e.layerY;
                }

                if (e.offsetX-scrollX < innerWidth/3)
                {
                    if (VerticalWritting)
                        nextImg();
                    else
                        preImg();
                } else if (e.offsetX-scrollX > innerWidth*2/3)
                {
                    if (VerticalWritting)
                        preImg();
                    else
                        nextImg();
                } else
                {
                    var p=parseInt(prompt("jump to 1-"+div_imgs.length+" page",c_page+1), 10);
                    if(p) {
                        if(p>div_imgs.length)
                            alert("maximum "+div_imgs.length+" pages!");
                        else {
                            c_page=p-1;
                            div_imgs[p-1].focusMe();
                        }
                    }
                }
            };
        }else
        {

            if (Landscape)
            {
                var __ScreenWidth = 1024;
                var __ScreenHeight= 690;
                var __RScreenWidth = 768;
                var __RScreenHeight = 946;
            } else
            {
                var __ScreenWidth = 768;
                var __ScreenHeight = 946;
                var __RScreenWidth = 1024;
                var __RScreenHeight= 690;
            }
            m.content="user-scalable=no,width=device-width,initial-scale=1.0,maximum-scale=1.0";
            _document.getElementsByTagName("head")[0].appendChild(m);
            _document.body.innerHTML="<div id=\"ReaderMain\" class=\"iReader\" style=\"width:"+__ScreenWidth+"px;height:"+__ScreenHeight+"px;overflow:hidden;position:absolute;\">";
            for(i=0; i<getPageCount(); ++i)
            {
                var div_img=_document.createElement("div");
                div_img.style.cssText="background-color:white;visibility:hidden;position:absolute;left:0px;top:0px;width:"+__ScreenWidth+"px;height:"+__ScreenHeight+"px;overflow-x:hidden;overflow-y:hidden;";
                div_img.page=i;
                div_img.tImg=new Image();
                div_img.appendChild(div_img.tImg);
                div_img.unloaded=true;
                div_img.eraseImg=function()
                {
                    if(!this.unloaded)
                    {
                        this.unloaded=true;
                        this.tImg.src=emptyImageSrc;
                    };
                };
                div_img.loadImg=function()
                {
                    var _this=this;
                    if(!this.unloaded)
                    {
                        if (!this.tImg.onload)
                        {
                            if (this.setToRight ^ !VerticalWritting)
                                this.tImg.left=this.tImg.width<=__ScreenWidth?(__ScreenWidth-this.tImg.width)/2:0;
                            else
                                this.tImg.left=this.tImg.width<=__ScreenWidth?(__ScreenWidth-this.tImg.width)/2:__ScreenWidth-this.tImg.width;
                            this.tImg.setTransform();
                        }
                        return;
                    }
                    this.unloaded=false;
                    this.tImg.onload=function()
                    {
                        var _w=this.width;
                        var _h=this.height;
                        if(_h>__ScreenHeight)
                        {
                            this.height=__ScreenHeight;
                            this.width=Math.floor(_w*__ScreenHeight/_h);
                        };
                        if (this.height<10){
                            this.top=0;
                            this.left=0;
                        } else{
                            if (_this.setToRight ^ !VerticalWritting)
                                this.left=this.width<=__ScreenWidth?(__ScreenWidth-this.width)/2:0;
                            else
                                this.left=this.width<=__ScreenWidth?(__ScreenWidth-this.width)/2:__ScreenWidth-this.width;
                            this.top =(__ScreenHeight - this.height)/2 ;
                        }
                        this.setTransform();
                        this.onload=null;
                        // clear the page
                        for (var NodeIdx = _document.body.childNodes.length-1; NodeIdx >=0; --NodeIdx)
                        {
                            if (_document.body.childNodes[NodeIdx].className !== "iReader")
                                _document.body.removeChild(_document.body.childNodes[NodeIdx]);
                        }
                    };
                    this.tImg.setTransform = function()
                    {
                        this.style.webkitTransform="translate("+ this.left+"px, "+this.top+"px)";
                    };
                    setTimeout(function() {
                        if (!_this.tImg.flink)
                            _this.tImg.flink=getImgURL(_this.page);
                        _this.tImg.src=_this.tImg.flink;
                    } ,0);
                    this.tImg.style.position="absolute";
                    this.tImg.left = this.tImg.top = 0;
                    this.tImg.setTransform();
                    // this.tImg.style.top="0px";
                    // this.tImg.style.left="0px";
                };
                div_img.focusMe=function(setToRight)
                {
                    setCurrentPage(this.page);
                    $_("currentPage").innerHTML=(this.page+1)+"/"+getPageCount();
                    $_("PageJump").value=this.page;
                    // _document.title = (this.page+1)+"/"+getPageCount() + " " + oldTitle;
                    try{
                        history.pushState({},(this.page+1)+"/"+getPageCount() + " " + oldTitle, getPageURL(this.page));
                    }
                    catch(e){ }
                    _document.title = (this.page+1)+"/"+getPageCount() + " " + oldTitle;

                    div_imgs[this.page].style.visibility="visible";
                    div_imgs[this.page].setToRight = setToRight;
                    div_imgs[this.page].loadImg(true);

                    for (var i=this.page+1; i<div_imgs.length;++i)
                    {
                        div_imgs[i].style.visibility="hidden";
                        div_imgs[i].setToRight = false;
                        if(i>this.page+3)
                            div_imgs[i].eraseImg();
                        else 
                            div_imgs[i].loadImg(false);
                    }
                    for (var i=this.page-1; i>=0;--i)
                    {
                        div_imgs[i].style.visibility="hidden";
                        div_imgs[i].setToRight = true;
                        if(i<this.page-2)
                            div_imgs[i].eraseImg();
                        else 
                            div_imgs[i].loadImg(false);
                    }

                    div_imgs[this.page].style.webkitTransform="translate(0px,0px)";

                    if (configuration.SmoothAnimation)
                        div_imgs[this.page].style.webkitTransitionProperty = "-webkit-transform";
                    if (this.page>0)
                        div_imgs[this.page-1].style.webkitTransform="translate(" + __ScreenWidth*(VerticalWritting?1:-1) +"px,0px)";
                    if (this.page < div_imgs.length -1)
                        div_imgs[this.page+1].style.webkitTransform="translate("+  __ScreenWidth*(VerticalWritting?-1:1) +"px,0px)";
                };

                div_img.tImg.addEventListener("webkitTransitionEnd",
                    function(){
                        // console.info("transitionEnd" + String(this.style.webkitTransitionProperty) );
                        this.style.webkitTransitionProperty = "none";
                    }, false );

                div_img.tImg.style.webkitTransitionDuration="400ms";
                div_img.tImg.style.webkitTransitionProperty = "none";

                div_img.style.webkitTransitionDuration="400ms";
                div_img.style.webkitTransitionProperty = "none";

                div_img.addEventListener("webkitTransitionEnd",
                    function(){
                        this.style.webkitTransitionProperty = "none";
                    }, false );

                div_postprocess(div_img);
                $_("ReaderMain").appendChild(div_img);
                div_imgs.push(div_img);
            };
            var div_options=_document.createElement("div");
            // div_options.style.cssText="font-size:25px;visibility:hidden;position:absolute;left:0px;top:0px;width:"+__ScreenWidth+"px;height:"+__ScreenHeight+"px;overflow-x:hidden;overflow-y:hidden;z-index:10;background-color:rgba(0,0,0,0.6);";
            div_options.style.cssText="font-size:25px;display:none;position:absolute;left:0px;top:0px;width:"+__ScreenWidth+"px;height:"+__ScreenHeight+"px;overflow-x:hidden;overflow-y:hidden;z-index:10;background-color:rgba(0,0,0,0.6);";
            div_options.enable = false;
            $_("ReaderMain").appendChild(div_options);
            div_options.innerHTML=formatStr("<input type='button' id='Reload' style='font-size:25px;position:absolute;top:25%;left:30%;' value='%s' />" +
                "<label style='position:absolute;top:30%;left:30%;background-color:black;color:white;'>" +
                "<input  type='checkbox' id='VerticalWritting'/>%s</label>" +
                "<div style='font-size:25px;position:absolute;top:35%;left:30%;background-color:black;color:white'>" +
                "%s<span id='currentPage'></span></div>" +
                "<div style='font-size:25px;position:absolute;top:40%;left:30%;background-color:black;color:white;'>" + 
                "%s<select id='PageJump'></select></div>" + 
                "<label style='position:absolute;top:45%;left:30%;background-color:black;color:white;'>" +
                "<input  type='checkbox' id='AnimationEnable'/>%s</label>" ,
                loc_RELOAD,
                loc_VERTICALWRITTING,
                loc_CURRENTPAGE,
                loc_JUMPTOPAGE,
                loc_ANIMATIONENABLE
            );
            div_options.innerHTML+="<div id='_left_chapter_' style='display:table;font-size:25px;position:absolute;left:0px;top:0px;width:25%;height:100%;background-color:rgba(255,0,0,0.5);color:white;'>"+
                                   "<span id='_left_chapter_text_' style='vertical-align: middle; display: table-cell;' ></span></div>";
            div_options.innerHTML+="<div id='_right_chapter_' style='display:table;font-size:25px;position:absolute;left:75%;top:0px;width:25%;height:100%;background-color:rgba(255,0,0,0.5);color:white;'>"+
                                   "<span id='_right_chapter_text_' style='vertical-align: middle; display: table-cell;' ></span></div>";
            div_options.innerHTML+="<div style='font-size:25px;position:absolute;top:90%;left:30%;background-color:black;color:white;'>Version 0.11.04.04.02</div>";

            var updateLeftRightPanel=function()
            {
                var nextChapter, nextChapterText, prevChapter, prevChapterText;
                if (VerticalWritting)
                {
                    nextChapter = $_("_left_chapter_");
                    nextChapterText = $_("_left_chapter_text_");
                    prevChapter= $_("_right_chapter_");
                    prevChapterText= $_("_right_chapter_text_");
                } else
                {
                    nextChapter = $_("_right_chapter_");
                    nextChapterText = $_("_right_chapter_text_");
                    prevChapter= $_("_left_chapter_");
                    prevChapterText= $_("_left_chapter_text_");
                }
                var next=getNextChapter();
                if (next)
                {
                    nextChapterText.innerHTML = formatStr(loc_NEXTCHAPTER, next.text);
                    nextChapter.onclick=function() { location.href = next.href; };
                    nextChapter.style.visibility = "visible";
                } else
                {
                    nextChapterText.innerHTML = "";//loc_NONEXTCHAPTER;
                    nextChapter.onclick=null;
                    nextChapter.style.visibility = "hidden";
                }
                var prev=getPrevChapter();
                if (prev)
                {
                    prevChapterText.innerHTML = formatStr(loc_PREVCHAPTER, prev.text);
                    prevChapter.onclick=function() { location.href = prev.href; };
                    prevChapter.style.visibility = "visible";
                } else
                {
                    prevChapterText.innerHTML = "";//loc_NOPREVCHAPTER;
                    prevChapter.onclick=null;
                    prevChapter.style.visibility = "hidden";
                }
            };
            $_("VerticalWritting").checked = VerticalWritting;
            $_("VerticalWritting").onclick=function()
            {
                VerticalWritting = !VerticalWritting;
                updateLeftRightPanel();
            };
            $_("Reload").onclick=function()
            {
                $___();
            };
            for(i=0; i<getPageCount(); ++i)
            {
                var newoption=_document.createElement("option");
                newoption.innerHTML=i+1;
                newoption.value=i;
                // if (i === getCurrentPage())
                //     newoption.setAttribute("selected", "selected");
                $_("PageJump").appendChild(newoption);
            };
            $_("PageJump").onchange=function()
            {
                c_page = $_("PageJump").value;
                div_imgs[c_page].focusMe();
                div_options.enable=false;
                // div_options.style.visibility = "hidden";
                div_options.style.display= "none";
            };
            $_("AnimationEnable").checked = !!configuration.SmoothAnimation;
            $_("AnimationEnable").onclick=function()
            {
                if (this.checked)
                {
                    if (!confirm(loc_ANIMATIONWARNING))
                        this.checked=false;
                }
                setConfig("SmoothAnimation", this.checked);
            };

            var showOptionPanel=function()
            {
                div_options.enable = true;
                // div_options.style.visibility = "visible";
                div_options.style.display= "inline";
                updateLeftRightPanel();
            };

            var div_rotationWarning = _document.createElement("div");
            div_rotationWarning.style.cssText="font-size:25px;visibility:hidden;position:absolute;left:0px;top:0px;width:"+__RScreenWidth+"px;height:"+__RScreenHeight+"px;overflow-x:hidden;overflow-y:hidden;z-index:20;background-color:rgba(0,0,0,0.6);";
            div_rotationWarning.innerHTML=formatStr("<div style='font-size:25px;position:absolute;top:45%;left:0px;width:100%;background-color:black;color:white;'>%s</div>",
                Landscape?loc_LANDSCAPEWARNING:loc_PORTRAITWARNING);

            div_rotationWarning.className="iReader";
            _document.body.appendChild(div_rotationWarning);
            var checkRotation=function()
            {
                var rightOrientation = true;
                if (_window.hasOwnProperty("orientation"))
                {
                    switch(_window.orientation)
                    {
                        case 0:
                        case 180:
                            rightOrientation = !Landscape;
                            break;
                        case 90:
                        case -90:
                            rightOrientation = Landscape;
                            break;
                    }
                }
                
                if (!rightOrientation)
                {
                    if (div_rotationWarning.style.visibility !== "visible")
                        div_rotationWarning.style.visibility = "visible";
                    div_rotationWarning.enable = true;
                } else
                {
                    if (div_rotationWarning.style.visibility !== "hidden")
                        div_rotationWarning.style.visibility = "hidden";
                    div_rotationWarning.enable = false;
                }
                window.scrollTo(0,0);
            };
            _window.onorientationchange=checkRotation;
            checkRotation();
            setInterval(checkRotation, 2500);


            var slideTowardRight=function()
            {
                if(div_imgs[c_page].tImg.left<0)
                {
                    var t_page = c_page;
                    div_imgs[c_page].tImg.left = 0;
                    div_imgs[c_page].tImg.setTransform();

                    if (configuration.SmoothAnimation)
                        div_imgs[c_page].tImg.style.webkitTransitionProperty = "-webkit-transform";

                    return true;
                };
                return false;
            };
            var slideTowardLeft=function()
            {
                if(div_imgs[c_page].tImg.width>__ScreenWidth && div_imgs[c_page].tImg.left===0)
                {
                    var t_page = c_page;
                    div_imgs[c_page].tImg.left=__ScreenWidth-div_imgs[c_page].tImg.width;
                    div_imgs[c_page].tImg.setTransform();
                    if (configuration.SmoothAnimation)
                        div_imgs[c_page].tImg.style.webkitTransitionProperty = "-webkit-transform";
                    return true;
                };
                return false;
            };

            var preImg=function()
            {

                if (VerticalWritting)
                {
                    if (slideTowardLeft())
                        return;
                } else
                {
                    if (slideTowardRight())
                        return;
                };
                --c_page;
                if(c_page<0)
                {
                    c_page=0;
                    // alert(loc_FIRSTPAGE);
                    showOptionPanel();
                }else 
                {
                    div_imgs[c_page].focusMe(true);
                    if (configuration.SmoothAnimation)
                    {
                        div_imgs[c_page+1].style.webkitTransitionProperty = "-webkit-transform";
                        div_imgs[c_page+1].style.visibility="visible";
                    }
                    // history.back();
                };
            };
            var nextImg=function()
            {
                if (VerticalWritting)
                {
                    if (slideTowardRight())
                        return;
                } else
                {
                    if (slideTowardLeft())
                        return;
                }
                ++c_page;
                if(c_page>=div_imgs.length)
                {
                    c_page=div_imgs.length-1;
                    // alert(loc_LASTPAGE);
                    showOptionPanel();
                }else 
                {
                    // history.forward();
                    div_imgs[c_page].focusMe();
                    if (configuration.SmoothAnimation)
                    {
                        div_imgs[c_page-1].style.webkitTransitionProperty = "-webkit-transform";
                        div_imgs[c_page-1].style.visibility="visible";
                    }
                }
            };
            if(navigator.userAgent.indexOf("iPad")!=-1)
            {
                _document.ontouchstart=function(e) {
                    return clickAt(e.touches[0].pageX, e.target);
                };
            }
            else
            {
                _document.onmousedown=function(e) {
                    clickAt(e.pageX, e.target);
                };
            };
            var clickAt=function(x, target)
            {
                if (div_rotationWarning.enable)
                    return;
                if (div_options.enable)
                {
                    if (target.tagName === "DIV")
                    {
                        div_options.enable=false;
                        // div_options.style.visibility = "hidden";
                        div_options.style.display= "none";
                        return false;
                    }
                    return;
                }

                if(x<__ScreenWidth/4)
                {
                    if (VerticalWritting)
                        nextImg();
                    else
                        preImg();
                }
                else if (x>__ScreenWidth*3/4)
                {
                    if (VerticalWritting)
                        preImg();
                    else
                        nextImg();
                }
                else
                {
                    showOptionPanel();
                }
                return false;

            };
        };

        c_page=getCurrentPage();
        div_imgs[c_page].focusMe();

        // c_page=getCurrentPage();
        // if (!_window.__iPadReaderInitialized)
        // {
        //     _window.__iPadReaderInitialized = true;
        //     var pageDelta = c_page - (getPageCount() -1);
        //     if (pageDelta != 0)
        //         history.go(pageDelta)
        //     else
        //         div_imgs[c_page].focusMe();
        // }else
        //     div_imgs[c_page].focusMe();
    },0);
})();
